<template>
  <div align="center">
    <div style="width: 300px;margin-top: 200px;">
      <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Login",
  data() {
    return {
      ruleForm: {}
    };
  },
  methods: {
    submitForm() {

      axios.post(this.$baseUrl+'/user/login', this.ruleForm).then(res => {
        if (res.data.flag) {
          // 将token 存入localStorage
          let userInfo = res.data.data
          window.localStorage.setItem("token", userInfo.token);
          this.$store.commit('setInfo', userInfo)
          this.$router.push("/")
        } else {
          alert(res.data.msg)
        }

      })
    }
  }
}
</script>

<style scoped>

</style>